package com.automannn.atm_back.modules.codeGenerator.controller;

import com.automannn.atm_back.common.api.vo.Result;
import com.automannn.atm_back.common.page.PageBean;
import com.automannn.atm_back.modules.codeGenerator.core.util.StringUtils;
import com.automannn.atm_back.modules.codeGenerator.entity.TableEntity;
import com.automannn.atm_back.modules.codeGenerator.environment.EnvironmentPerception;
import com.automannn.atm_back.modules.codeGenerator.service.SchemaEntityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenkh
 * @time 2020/6/29 20:03
 */
@RestController
@RequestMapping("codeGenerator/codeGenerator/schema")
@Slf4j
public class SchemaEntityController {

    @Resource
    private SchemaEntityService schemaEntityService;

    /**
     * 查询所有的表格信息
     *
     * @return
     */
    @PostMapping("queryTables")
    public Result<IPage<TableEntity>> queryTables(@RequestBody PageBean pageBean) {
        Result result = new Result();
        if (EnvironmentPerception.getDatasourceType() == null) {
            log.error("未检测到数据库源，请调用其它数据源接口");
            throw new RuntimeException("未找到数据源信息!");
        }
        IPage<TableEntity> pageList = schemaEntityService.queryAllTables(pageBean.toPage(), EnvironmentPerception.getNamespace());
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 查询表格的列信息
     *
     * @return
     */
    @PostMapping("queryColumns")
    public Map queryColumns(String tableName) {
        if (StringUtils.isEmpty(tableName)) throw new RuntimeException("请传入正确的表名");
        List list = schemaEntityService.queryAllColumn(tableName);
        Map map = new HashMap();
        map.put("data", list);
        map.put("success", true);
        return map;
    }


}
